package com.zhihui.online.utils;

import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePrecreateModel;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;


/**
 * 阿里支付二维码工具
 */
@Component
public class ALiPayUtil {
    private static final String FORMAT = "JSON";
    private static final String CHARSET = "UTF-8";
    //签名方式
    private static final String SIGN_TYPE = "RSA2";

    @Autowired
    private PayConfig payConfig;
    private  AlipayConfig getAlipayConfig() {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl(payConfig.getGatewayUrl());
        alipayConfig.setAppId(payConfig.getAppId());
        alipayConfig.setPrivateKey(payConfig.getAppPrivateKey());
        alipayConfig.setFormat(FORMAT);
        alipayConfig.setAlipayPublicKey(payConfig.getAlipayPublicKey());
        alipayConfig.setCharset(CHARSET);
        alipayConfig.setSignType(SIGN_TYPE);
        return alipayConfig;
    }




    public byte[] getPayQR(Long orderNo) throws AlipayApiException {
        // 初始化SDK
        AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());
        // 构造请求参数以调用接口
        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
        AlipayTradePrecreateModel model = new AlipayTradePrecreateModel();
        // 设置订单标题
        model.setSubject("阿良TV会员充值");
        // 设置商户订单号
        model.setOutTradeNo(String.valueOf(orderNo));
        // 设置订单总金额
        model.setTotalAmount("6.6");
        request.setBizModel(model);
        //设置异步回调地址
        request.setNotifyUrl(payConfig.getNotifyBaseUrl() + payConfig.getNotifyUrl());
        AlipayTradePrecreateResponse response = alipayClient.execute(request);
        System.out.println(response.getBody());
        if (response.isSuccess()) {
            //获得二维码支付地址
            JSONObject bean = JSONUtil.toBean(response.getBody(), JSONObject.class);
            JSONObject realData = (JSONObject) bean.get("alipay_trade_precreate_response");
            String qrCode = (String) realData.get("qr_code");
            //生成二维码
            BufferedImage generate = QrCodeUtil.generate(//
                    qrCode, //二维码内容
                    QrConfig.create().setImg(payConfig.getLogoUrl())//附带logo
            );
            ByteArrayOutputStream outputStream  = new ByteArrayOutputStream();
            //返回数据给后端
            try {
                ImageIO.write(generate, "png", outputStream);
                return outputStream.toByteArray();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        } else {
           throw new RuntimeException("支付宝接口调用失败");
        }
    }


}